package com.jsu.clubmanagementsystem.service.club.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.PageRequest;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.entity.ClubMembership;
import com.jsu.clubmanagementsystem.mapper.ClubMembershipMapper;
import com.jsu.clubmanagementsystem.model.vo.club.ListMemberVO;
import com.jsu.clubmanagementsystem.service.club.ClubMembershipService;
import com.jsu.clubmanagementsystem.service.club.ClubService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 24753
* @description 针对表【club_membership(社团人员关系)】的数据库操作Service实现
* @createDate 2025-05-13 17:19:16
*/
@Service
public class ClubMembershipServiceImpl extends ServiceImpl<ClubMembershipMapper, ClubMembership>
        implements ClubMembershipService {

    @Resource
    private ClubService clubService;

    /**
     * 用户是否是目标社团的成员
     * @param userId
     * @param clubId
     * @return
     */
    public boolean isMemberOfClub(Long userId, Long clubId) {
        QueryWrapper<ClubMembership> queryWrapper = getQueryWrapper(userId, clubId);
        return this.count(queryWrapper) > 0;
    }

    /**
     * 用户是否同时加入少于三个社团
     * @param userId
     * @return
     */
    @Override
    public boolean isUserInLessThan3Club(long userId) {
        QueryWrapper<ClubMembership> queryWrapper = this.getQueryWrapper(userId, 0L);
        return this.count(queryWrapper) < 3;
    }

    /**
     * 获得查询封装
     *
     * @param userId
     * @param clubId
     * @return
     */
    public QueryWrapper<ClubMembership> getQueryWrapper(Long userId, Long clubId) {
        //需要确保参数存在
        QueryWrapper<ClubMembership> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(clubId > 0, "clubId", clubId);
        queryWrapper.eq(userId > 0, "userId", userId);
        queryWrapper.orderByAsc("identity");  // 默认按身份的升序排序
        return queryWrapper;
    }

    @Override
    public Page<ListMemberVO> getListMemberVO(PageRequest page) {
        Page<ClubMembership> iPage=new Page<>(page.getCurrent(),page.getPageSize());
        //获取所有社团关系
        List<ClubMembership> clubMemberships = this.baseMapper.selectList(iPage, null);
        long sum=0L;
        if(clubMemberships.size()<=page.getPageSize()){
            sum=((long) (page.getCurrent()-1) *page.getPageSize())+clubMemberships.size();
        }
        return new Page<ListMemberVO>(page.getCurrent(),page.getPageSize()).setRecords(clubMemberships.stream().map(
                userId->{
                    ListMemberVO  listMemberVO = new ListMemberVO();
                    BeanUtils.copyProperties(userId,listMemberVO);
                    listMemberVO.setClub(clubService.getBaseMapper().selectById(listMemberVO.getClubId()));
                    return listMemberVO;
                }
        ).toList()).setTotal(sum);
    }

    @Override
    public boolean addUserToClub(Long userId, Long clubId) {
        if(this.isMemberOfClub(userId, clubId)){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户已经是该社团的成员");
        }
        ClubMembership membership = new ClubMembership();
        membership.setUserId(userId);
        membership.setClubId(clubId);
        this.save(membership);
        return true;
    }

    @Override
    public void quitClub(Long userId, Long clubId) {
        QueryWrapper<ClubMembership> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).eq("clubId", clubId);
        this.baseMapper.delete(queryWrapper);
    }
}




